package com.sanfu.etl.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.client.Put;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class HBaseAnnotationAnalysis {
    // 获取tableName
    public String analysisTableName(Object obj) {
        HBaseTable table = obj.getClass().getAnnotation(HBaseTable.class);
        return table.tableName();
    }

    // 获取自定义Entity类的rowkey属性值
    public <T> String analysisRowkey(T obj){
        try {
            Class<?> clazz = obj.getClass();
            Field rowkey = clazz.getDeclaredField("rowkey");
            rowkey.setAccessible(true);

            if ( !rowkey.isAnnotationPresent(HBaseColumn.class)){
                return obj.toString();
            }

            Object fieldValue = rowkey.get(obj); // 取得对象的Field属性值

            return fieldValue.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    //
    public <T> T putEncapsulation(T obj) {
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 过滤非HBase注解字段
            if (!field.isAnnotationPresent(HBaseColumn.class)) {
                continue;
            }

            // 获取属性上的指定注解【HBaseColumn】的属性值
            HBaseColumn orm = field.getAnnotation(HBaseColumn.class);
            String family = orm.family();
            String qualifier = orm.qualifier();

            // 过滤空值
            if (!StringUtils.isBlank(family) || !StringUtils.isBlank(qualifier)) {
                return obj;
            }

            String rowkey = analysisRowkey(obj);
        }
        return obj;
    }
}
